/*
 * @Descripttion: 
 * @version: 
 * @Author: Jinjin
 * @Date: 2022-04-28 10:01:03
 * @LastEditors: Jinjin
 * @LastEditTime: 2022-05-22 15:47:30
 */
// 这个文件专门用来存放关于评论的所有接口

const comment=require('koa-router')()
const db=require("../util/db")


// http://api.lzjlw.top:3456/comment/interactive_Comments/${videoId}
comment.get("/interactive_Comments/:videoId",async (ctx)=>{
    // 通过ctx.params获取url中的":videoId"部分（结果为对象）
    let videoId=ctx.params.videoId
    console.log(ctx.params.videoId)
    let selectSql=`select commentId,videoId,user.userId,content,type,public_Time,userName from interactive_Comments,user where videoId=${videoId} and interactive_Comments.userId=user.userId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                if(result!=[]){
                    resolve(result)
                }
                else{
                    resolve('Not Found')
                }
            }
        })
    })
    ctx.body=res
})


// http://api.lzjlw.top:3030/comment/interactive_Comments
comment.post("/interactive_Comments",async (ctx)=>{
    // 生成唯一commentId(bigint)
    let left_commentId=(new Date().getTime().toString())
    let commentId=left_commentId+=Math.floor((Math.random()*10000))
    let obj=ctx.request.body
    console.log(obj)
    let public_Time=new Date().getTime()
    let insertSql=`insert into interactive_Comments(commentId,videoId,userId,content,type,public_Time) 
    values(${commentId},${obj.videoId},${obj.userId},'${obj.content}',${obj.type},${public_Time})`
    let res=await new Promise((resolve,reject)=>{
        db.query(insertSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve('insert success')
            }
        })
    })
    ctx.body=res
})


module.exports=comment